<?php 

class TransactionCentral_Card_Transaction extends TransactionCentral_Transaction implements TransactionCentral_Interface_CreditDebitTransaction
{
    protected $trans_id = '';
    
	protected $amount = '';
	protected $card_number = '';
	protected $expiration = '';
	protected $ref_id = '';
	
	//Optional fields
	protected $card_holder_name = '';
	protected $address = '';
	protected $zip_code = '';
	protected $track_data = '';
	
	public function process($refund_or_debit)
	{
	    if($refund_or_debit == 'Refund') {
            $ach_credit = new TransactionCentral_Card_Transaction_Credit($this);
            return $ach_credit->process();
        } elseif ($refund_or_debit == 'Debit') {
            $ach_debit = new TransactionCentral_Card_Transaction_Debit($this);
            return $ach_debit->process();
        } else {
            throw new Exception(get_class($this) . '::' . __FUNCTION__ . ' must be passed either "Credit" or "Debit"');
        }			
	}
	
    public function getStatus($param)
    {
        if(is_array($param))
	    {
	        if(isset($param['ref_id']) || isset($param['posted_date']) || isset($param['amount']))
		    {
		       return TransactionCentral_CurlTransaction::curlTransaction(self::CC_TRANS_URL,
                            		                                      self::CC_STATUSREF, array(
                                        															'MerchantID'=> TransactionCentral_Transaction::MERCH_ID,
                                        		                                                    'RegKey'	=> TransactionCentral_Transaction::REG_KEY,
                                        															'RefId' 	=>	$param['ref_id'],
                                        															'PostedDate'=>	$param['posted_date'],
                                        															'Amount' 	=>	$param['amount']		
                                    														    ));
		    } else {
		        throw new Exception('Must pass ' . get_class($this) . '::' . __FUNCTION__ . ' either TransID or an array of ref_id, posted_date, and amount.'); 
		    }
	    } else {
	        if($param == '' || $param == null) {
	            return TransactionCentral_CurlTransaction::curlTransaction(self::CC_TRANS_URL,
	                                                                       self::CC_STATUS, array(
                                	                                                            'MerchantID'=> TransactionCentral_Transaction::MERCH_ID,
                                        		                                                'RegKey'	=> TransactionCentral_Transaction::REG_KEY,
                                        														'TransID' 	=>	$param
                                        													   ));
	        }
	    }
    }
    
    public function verify($cardNumber, $expirationDate)
	{
		if($cardNumber == '' || $cardNumber == null || $expirationDate == '' || $expirationDate == null) {
			throw new Exception('Must pass ' . get_class($this) . __FUNCTION__ . ' CardNumber and Expiration.');
		}
		
		return TransactionCentral_CurlTransaction::curlTransaction(self::CC_TRANS_URL,
                            		                               self::CC_VERIFY, array(
    		                                                                            'MerchantID'=> TransactionCentral_Transaction::MERCH_ID,
            		                                                    				'RegKey'	=> TransactionCentral_Transaction::REG_KEY,
                            															'CardNumber' 	    =>	$cardNumber,
                            															'Expiration'	    =>	$expirationDate,
                            															'CardHolderName'    =>	'',
                            															'CVV2'			    =>	'',
                            															'RefId' 		    =>	'',
                            															'Address'		    =>	'',
                            															'ZipCode' 		    =>	'',
                            															'PaymentDesc'	    =>	'',
                            															'MerchZIP' 		    =>	'',
                            															'MerchCustPNum'	    =>	'',
                            															'MCC' 			    =>	'',
                            															'POSInd'		    =>	'',
                            															'POSConditionCode'  =>	'',
                            															'EComInd'		    =>	'',
                            															'AuthCharInd' 	    =>	'',
                            															'CardCertData'	    =>	'',
                            															'CAVVData' 		    =>	'',
                            															'UserId'		    =>	'',
                            															'TrackData' 	    =>	''
                        														      ));
	}
    
	public function void($trans_id)
	{
		if($trans_id == '' || $trans_id == null) {
			throw new Exception('Must pass ' . get_class($this) . '::' . __FUNCTION__ . ' TransId.');
		}
		
		return TransactionCentral_CurlTransaction::curlTransaction(TransactionCentral_Transaction::ACH_TRANS_URL,
		                                                           TransactionCentral_Transaction::CC_VOID, array(
                                                    		                                                    'MerchantID'=> TransactionCentral_Transaction::MERCH_ID,
                                                            		                                            'RegKey'	=> TransactionCentral_Transaction::REG_KEY,
                                                    															'TransID' 	=>	$trans_id
                                                        														));
                                	}
	
	/*
	 * Used to refund transactions (or portions of transactions) AFTER settling occurs.
	 * 
	 * Since this is actually a seperate transaction of its own (generates its own TransID), it may need to be a 
	 * class of its own at some point. But, unless there are unforseen issues, it will be fine as a static method
	 * here for now.
	 */
	public static function creditBackTransaction($trans_id, $amount, $ref_id = null)
	{
	    if($trans_id == null || $trans_id == '' || $amount == null || $amount == '') {
	        throw new Exception('Must pass ' . get_class($this) . '::' . __FUNCTION__ . 'at least TransID and Amount.');
	    }
	    
	    return self::curlTransaction(self::ACH_TRANS_URL,
	                                 self::CC_CREDIT, array(
	                                                            'MerchantID'=> TransactionCentral_Transaction::MERCH_ID,
        		                                                'RegKey'	=> TransactionCentral_Transaction::REG_KEY,
	                                                            'TransID'	=> $trans_id,
	                                                            'Amount'	=> $amount,
	                                                            'RefID'		=> $ref_id
	                                                        ));
	}
	
	
	
	
	
	
	
	
	
	
	
	public function getAddress() 
	{ 
		return $this->address; 
	}

	public function setAddress($address) 
	{ 
		$this->address = $address;
		return $this; 
	}

	public function getAmount() 
	{ 
		return $this->amount; 
	}

	public function setAmount($amount) 
	{ 
		$this->amount = $amount;
		return $this; 
	}

	public function getCardHolderName() 
	{ 
		return $this->card_holder_name; 
	}

	public function setCardHolderName($card_holder_name) 
	{ 
		$this->card_holder_name = $card_holder_name;
		return $this; 
	}

	public function getCardNumber() 
	{ 
		return $this->card_number; 
	}

	public function setCardNumber($card_number)
	{ 
		$this->card_number = $card_number; 
		return $this;
	}

	public function getExpiration() 
	{ 
		return $this->expiration; 
	}

	public function setExpiration($expiration) 
	{ 
		$this->expiration = $expiration; 
		return $this;
	}

	public function getRefId() 
	{ 
		return $this->ref_id; 
	}

	public function setRefId($ref_id) 
	{ 
		$this->ref_id = $ref_id; 
		return $this;
	}

	public function getTrackData() 
	{ 
		return $this->track_data; 
	}

	public function setTrackData($track_data) 
	{ 
		$this->track_data = $track_data; 
		return $this;
	}

	public function getZipCode() 
	{ 
		return $this->zip_code; 
	}

	public function setZipCode($zip_code) 
	{ 
		$this->zip_code = $zip_code;
		return $this;
	}
	
	public function getTransId()
	{
	    return $this->trans_id;
	}
	
	public function setTransId($trans_id)
	{
	    $this->trans_id = $trans_id;
	    return $this;
	}
}